package com.hnkywl.sts.service.cwgl.kmys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.kmys.LjkmdmysDao;
import com.hnkywl.sts.entity.cwgl.kmys.Ljkmdmys;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-13
 * Time: 上午11:27
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LjkmdmysService {
    private LjkmdmysDao ljkmdmysDao;

    public String findLjkmdmysList(String qyjgQc, String gysfzhsdm, String khfzhsdm, int pageNum, int pageSize) {
        Page<Map> page = ljkmdmysDao.findAlls(qyjgQc, gysfzhsdm, khfzhsdm, pageNum, pageSize);
        List result = page.getResult() != null ? page.getResult() : new ArrayList();
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(result) + "}";
    }

    public String findDetail(Long id) {
        return new Gson().toJson(ljkmdmysDao.getByID(id));
    }

    public String findKmdm(Long gsId, Integer type) {
        Gson gson = new Gson();
        return gson.toJson(ljkmdmysDao.findKmdm(gsId, type));
    }

    @Transactional
    public Long save(Ljkmdmys ljkmdmys) {
        if (ljkmdmys != null && ljkmdmys.getId() != null) {
            Ljkmdmys ljkmdmy = ljkmdmysDao.get(ljkmdmys.getId());
            ljkmdmy.setGysfzhsdm(ljkmdmys.getGysfzhsdm());
            ljkmdmy.setKhfzhsdm(ljkmdmys.getKhfzhsdm());
            ljkmdmy.setType(ljkmdmys.getType());
            ljkmdmysDao.save(ljkmdmy);
        } else {
            ljkmdmysDao.save(ljkmdmys);
        }
        return ljkmdmys.getId();
    }

    @Autowired
    public void setLjkmdmysDao(LjkmdmysDao ljkmdmysDao) {
        this.ljkmdmysDao = ljkmdmysDao;
    }
}
